﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using MINERP.InfoConfig.Controller;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using MINERP.COM;

namespace MINERP.InfoConfig
{
    public partial class FrmPaperTypeManager : FrmMDIChildFormTemplate 
    {
        private PaperTypeController _controller = new PaperTypeController();
        private paperType _paperType = null;
        static FrmPaperTypeManager instance = null;
        private FrmPaperTypeManager()
        {
            InitializeComponent();
        }
        public static FrmPaperTypeManager CreateInstance
        {
            get
            {
                instance = new FrmPaperTypeManager();
                return instance;
            }
        }

        private void FrmPaperTypeManager_Load(object sender, EventArgs e)
        {

            _controller.Grid = gridControlPaperType;
            _controller.View = gridViewPaperType;
            _controller.GetDate();

            _controller.ComoBox = comboBoxDefaultBordWidth;


        }

        private void gridViewPaperType_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperType = (paperType)gridViewPaperType.GetFocusedRow();
            if (_paperType != null)
            {
                txtPaperTypeName.Text = _paperType.Name;
                txtRemark.Text = _paperType.remark;
                comboBoxDefaultBordWidth.Text = _controller.GetDefaultBordWidth(_paperType);

                _controller.Grid = gridControlBordWidth;
                _controller.View = gridViewBordWidth;
                _controller.LinkBordWidthFocuseChanged(_paperType.PaperTypeID);
            }

        }
        //添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtPaperTypeName.Focus();
                return;
            }
            if (CheckData())
            {
                paperType type = new paperType();
                type.Name = txtPaperTypeName.Text;
                type.remark = txtRemark.Text;
                type.Status = 1;
                _controller.Add(type);

                //link datagrid
                _controller.Grid = gridControlPaperType;
                _controller.View = gridViewPaperType;
                _controller.GetDate();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //更新
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtPaperTypeName.Focus();
                return;
            }
            if (CheckData())
            {
                paperType type = _controller.GetPaperTypeObj(_paperType.PaperTypeID);
                type.Name = txtPaperTypeName.Text;
                type.remark = txtRemark.Text;
                _controller.Update(type);

                //refresh datagrid
                _controller.Grid = gridControlPaperType;
                _controller.View = gridViewPaperType;
                _controller.GetDate();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (_paperType.Name != "任意")
            {
                if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    paperType type = _controller.GetPaperTypeObj(_paperType.PaperTypeID);
                    _controller.Delete(type);

                    //refresh datagrid
                    _controller.Grid = gridControlPaperType;
                    _controller.View = gridViewPaperType;
                    _controller.GetDate();
                }
            }
            else
            {
                MessageBox.Show("系统数据不能删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtPaperTypeName.Enabled = false;
            txtRemark.Enabled = false;
            comboBoxDefaultBordWidth.Enabled = false;
            groupControlBoardWidth.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtPaperTypeName.Text == string.Empty)
            {
                txtPaperTypeName.Focus();
                MessageBox.Show("纸型名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtRemark.Text == string.Empty)
            {
                txtRemark.Focus();
                MessageBox.Show("备注不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (comboBoxDefaultBordWidth.Text == string.Empty)
            {
                comboBoxDefaultBordWidth.Focus();
                MessageBox.Show("默认纸型宽不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {

            txtPaperTypeName.Enabled = bl;
            txtRemark.Enabled = bl;
            comboBoxDefaultBordWidth.Enabled = bl;
            groupControlBoardWidth.Enabled = bl;

            txtPaperTypeName.Text = string.Empty;
            txtRemark.Text = string.Empty;
            comboBoxDefaultBordWidth.Text = string.Empty;
            if (bl)
            {
                _controller.Grid = gridControlBordWidth;
                _controller.View = gridViewBordWidth;
                _controller.LinkBordWidth();
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtPaperTypeName.Enabled = bl;
            txtRemark.Enabled = bl;
            comboBoxDefaultBordWidth.Enabled = bl;
            groupControlBoardWidth.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmPaperTypeManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void gridViewBordWidth_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            _controller.LinkDefaultBordWidth();
        }
    }
}
